package com.yunji.product.business;

import com.yunji.common.base.BaseBusiness;
import com.yunji.common.enums.common.ErrorEnum;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.product.domain.ProductUnitDO;
import com.yunji.product.params.UnitParam;
import com.yunji.product.service.IProductService;
import com.yunji.product.service.IProductUnitService;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Project: yunji-coffee
 * @Package: com.yunji.product.business
 * @Title: ProductUnitBusiness
 * <p>
 * History:
 * Date                     Version     Author          Summary
 * ============================================================
 * 2024-08-13 17:23:52      V1.0        HuaAo       新建类
 */
@Component
public class ProductUnitBusiness extends BaseBusiness {

    @Resource
    IProductUnitService productUnitService;

    @Resource
    IProductService productService;

    /**
     * @Title 获取单位列表
     * @Desc
     * @Date 2024-08-13 18:11:42.852
     * @Return null
     */
    public List<ProductUnitDO> search(UnitParam param) {
        return productUnitService.search(param);
    }

    /**
     * @Title 新增商品单位
     * @Desc
     * @Date 2024-08-13 17:26:45.779
     * @Return null
     */
    public Long create(ProductUnitDO productUnit) {
        checkParam(productUnit);
        Assert.isTrue(productUnitService.create(productUnit), "新增失败");
        return productUnit.getUnitId();
    }

    /**
     * @Title 查看商品单位详情
     * @Desc
     * @Date 2024-08-13 17:55:51.995
     * @Return null
     */
    public ProductUnitDO select(Long id) {
        return productUnitService.select(id);
    }

    /**
     * @Title 跟新商品品牌
     * @Desc
     * @Date 2024-08-14 11:52:56.465
     * @Return null
     */
    public void update(ProductUnitDO productUnit) {
        checkParam(productUnit);
        Assert.isTrue(productUnitService.update(productUnit), "修改失败");
    }

    public void delete(Long... ids) {
        // 有限制条件 有商品应用则无法删除
        Assert.isTrue((productService.statsOfProductUnit(ids) == 0), ErrorEnum.BUSINESS_512002.getText());

        productUnitService.delete(ids);
    }


    public Boolean inUse(Long unitId) {
        return (productService.statsOfProductUnit(unitId) > 0) ? Boolean.TRUE : Boolean.FALSE;
    }

    /*-------------------------------------------------------------------------校验-------------------------------------------------------------------------*/

    private void checkParam(ProductUnitDO productUnit) {
        Assert.isTrue(BeanUtils.isNotNull(productUnit.getUnitName()), "请输入商品单位");
        Assert.isTrue(BeanUtils.isNotNull(productUnit.getSort()), "请输入排序序号");

        String unitName = productUnit.getUnitName();
        Integer sort = productUnit.getSort();

        logs.info("【参数校验】商品单位:{}", unitName);
        logs.info("【校验参数】排序序号:{}", sort);
    }
}